www.gusucode.com > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序 > DB-Libray 操作SQLServer编程的VC++ 一例-源码程序\code\PhManage\OfficeDlg.cpp

    //Download by http://www.NewXing.com
// OfficeDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PhysicM.h"
#include "OfficeDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COfficeDlg dialog


COfficeDlg::COfficeDlg(CWnd* pParent /*=NULL*/)
	: CDialog(COfficeDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COfficeDlg)
	m_demo = _T("");
	m_jc = _T("");
	m_name = _T("");
	//}}AFX_DATA_INIT
}


void COfficeDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COfficeDlg)
	DDX_Control(pDX, IDC_OFFICE_LIST, m_List);
	DDX_Text(pDX, IDC_DEMO_EDIT, m_demo);
	DDX_Text(pDX, IDC_OFFICE_JC, m_jc);
	DDX_Text(pDX, IDC_OFFICE_NAME, m_name);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COfficeDlg, CDialog)
	//{{AFX_MSG_MAP(COfficeDlg)
	ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
	ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COfficeDlg message handlers

void COfficeDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//CDialog::OnOK();
}

BOOL COfficeDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_List.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
	char *head[]={"序 号","科室名称","科室简称","科室ID","备注"};
	int i;
	for (i=0;i<5;i++)
		m_List.InsertColumn(i,head[i],LVCFMT_LEFT,(i==1) ? 150 : 100,i);

	ImageList=new CImageList();
	ImageList->Create(16,16,TRUE | ILC_COLOR32,2,0);
	ImageList->Add(AfxGetApp()->LoadIcon(IDI_ICONUSER));
	ImageList->Add(AfxGetApp()->LoadIcon(IDR_MENUVIEW_TMPL));
	m_List.SetImageList(ImageList,LVSIL_SMALL);// LVSIL_NORMAL);
	Init();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
void COfficeDlg::Init()
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	try{
		DB.Open("SELECT office_id oid ,office_name fn,office_jc oj,demo dm from office order by office_id");
		int id=0,no=0;;
		char tmp[50];
		memset(tmp,0,50);
		while(!DB.isEof())
		{
			sprintf(tmp,"%03d",no+1);
			m_List.InsertItem(no,tmp,0);
			DB.GetValue("fn",tmp);
			m_List.SetItemText(no,1,(LPCTSTR)tmp);
			DB.GetValue("oj",tmp);
			m_List.SetItemText(no,2,(LPCTSTR)tmp);
			id=DB.GetValue("oid");
			sprintf(tmp,"%03d",id);
			m_List.SetItemText(no,3,(LPCTSTR)tmp);
			memset(tmp,0,50);
			DB.GetValue("dm",tmp);
			m_List.SetItemText(no,4,(LPCTSTR)tmp);
			no++;
			DB.Next();
		}
	}catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch (...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}

}
void COfficeDlg::AddInfo(CString m_pname,CString m_pj,CString m_dm)
{
	DBLibrary DB(((CPhysicMApp *)AfxGetApp())->DBSession);
	int max_id=0;
	try{
		DB.Open("select IDENT_CURRENT('office') did "
			" from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='office' ");
		double id=0;
		DB.GetValue("did",&id);
		max_id=(int)id+1;
		DB.ExecSQL("INSERT INTO Office (Office_name,Office_jc,demo) "
			"VALUES('%s','%s','%s')",m_pname,m_pj,m_dm);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddLog("新增科室:%s",m_pname);
		char no[4];
		int icount=m_List.GetItemCount();
		memset(no,0,4);
		sprintf(no,"%03d",icount+1);
		m_List.InsertItem(m_List.GetItemCount(),no,0);
		m_List.SetItemText(icount,1,(LPCTSTR)m_pname);
		m_List.SetItemText(icount,2,(LPCTSTR)m_pj);
		sprintf(no,"%03d",max_id);
		m_List.SetItemText(icount,3,(LPCTSTR)no);
		m_List.SetItemText(icount,4,(LPCTSTR)m_dm);

	}
	catch (DBErr &err)
	{
		char *str;
		int code;
		err.GetLastErr(code,&str);
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("%s(%d)",str,code);
	}
	catch(...)
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("unknow err(%d)",-1);
	}
}

BOOL COfficeDlg::DestroyWindow() 
{
    if (ImageList) delete ImageList;	
	return CDialog::DestroyWindow();
}

void COfficeDlg::OnAddButton() 
{
	UpdateData();
	m_name.TrimLeft();
	m_name.TrimRight();
	if(m_name.IsEmpty())
	{
		((CPhysicMApp *)AfxGetApp())->pMainFrm->AddErr("科室名称不能为空,请输入");
		return;
	}
	AddInfo(m_name,m_jc,m_demo);	
}

void COfficeDlg::OnDeleteButton() 
{
	((CPhysicMApp *)AfxGetApp())->Delete("Office","Office_id",&m_List,3);
}